package com.longdaji.decoration;


import android.app.Activity;
import android.text.TextUtils;

import com.longdaji.decoration.di.component.DaggerAppComponent;
import com.longdaji.decoration.utils.PreferencesUtil;
import com.longdaji.decoration.yunxin.NimDemoLocationProvider;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.api.UIKitOptions;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.util.NIMUtil;
import com.tendcloud.tenddata.TCAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;

import java.util.ArrayList;
import java.util.List;

import dagger.android.AndroidInjector;
import dagger.android.DaggerApplication;

/**
 * Created by longtao on 2017/12/26.
 */

public class DecorationApplication extends DaggerApplication {
    private static DecorationApplication instance;

    private static boolean isJustLook = false;

    public static synchronized DecorationApplication getInstance() {
        return instance;
    }

    private static List<Activity> mDestroyActivityList = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        TCAgent.LOG_ON = true;
        TCAgent.init(this.getApplicationContext(), Constants.TALKING_DATA_APP_ID, "default");
        TCAgent.setReportUncaughtExceptions(true);
        //设置LOG开关，默认为false
        UMConfigure.setLogEnabled(true);
        //初始化组件化基础库, 统计SDK/推送SDK/分享SDK都必须调用此初始化接口
        UMConfigure.init(this, "5af708c18f4a9d7bdd0003b3", "Umeng", UMConfigure.DEVICE_TYPE_PHONE,
                null);

        // 4.6.0 开始，第三方推送配置入口改为 SDKOption#mixPushConfig，旧版配置方式依旧支持。
        NIMClient.init(this, getLoginInfo(), NimSDKOptionConfig.getSDKOptions(this));
        // 以下逻辑只在主进程初始化时执行
        if (NIMUtil.isMainProcess(this)) {
            // 初始化UIKit模块
            initUIKit();

        }
    }

    private void initUIKit() {
        // 初始化
        NimUIKit.init(this, buildUIKitOptions());

        // 设置地理位置提供者。如果需要发送地理位置消息，该参数必须提供。如果不需要，可以忽略。
        NimUIKit.setLocationProvider(new NimDemoLocationProvider());
//
//        // IM 会话窗口的定制初始化。
//        SessionHelper.init();
//
//        // 聊天室聊天窗口的定制初始化。
//        ChatRoomSessionHelper.init();
//
//        // 通讯录列表定制初始化
//        ContactHelper.init();
//
//        // 添加自定义推送文案以及选项，请开发者在各端（Android、IOS、PC、Web）消息发送时保持一致，以免出现通知不一致的情况
//        NimUIKit.setCustomPushContentProvider(new DemoPushContentProvider());
//
//        NimUIKit.setOnlineStateContentProvider(new DemoOnlineStateContentProvider());
    }

    private UIKitOptions buildUIKitOptions() {
        UIKitOptions options = new UIKitOptions();
        // 设置app图片/音频/日志等缓存目录
        options.appCacheDir = NimSDKOptionConfig.getAppCacheDir(this) + "/app";
        return options;
    }

    private LoginInfo getLoginInfo() {
        String account = PreferencesUtil.getUserAccount();
        String token = PreferencesUtil.getUserToken();

        if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(token)) {
            return new LoginInfo(account, token);
        } else {
            return null;
        }
    }

    @Override
    protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
        return DaggerAppComponent.builder().application(this).build();
    }

    //活动控制
    public static void addDetroyActivity(Activity activity) {
        mDestroyActivityList.add(activity);
    }

    public static void clearDestroyList() {
        mDestroyActivityList.clear();
    }

    public static void destroySelectedActivities() {
        for (Activity activity : mDestroyActivityList) {
            if (activity != null)
                activity.finish();
        }
        clearDestroyList();
    }

    //随便看看
    public static void setJustLook(boolean justLook) {
        isJustLook = justLook;
    }

    public static boolean getJustLook() {
        return isJustLook;
    }

    {
        PlatformConfig.setWeixin("wxcf4b5342751eeada", "de6c8d3f6c65452c08ebd72c46cf5dbc");
        PlatformConfig.setQQZone("1106827415", "lsPR4R3MrFIVcEYL");

    }
}
